package cn.wolfcode;

import cn.wolfcode.shiro.UserRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

/**
 * ClassName testAuthcByRealm
 * Create by Mike
 * Date 2021/8/15 14:13
 */
public class testAuthcByRealm {

    @Test
    public void testAuthcByRealm(){
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        // 注册自定义 Realm
        securityManager.setRealm(new UserRealm());
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        System.out.println("认证状态：" + subject.isAuthenticated());
        UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "555");
        subject.login(token);
        System.out.println("认证状态：" + subject.isAuthenticated());
        // 登出
        subject.logout();
        System.out.println("认证状态：" + subject.isAuthenticated());
    }
}
